package com.girl.handle;

import com.girl.domain.Result;
import com.girl.exception.GrilExecption;
import com.girl.units.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 捕获异常类(最外层抛出的异常)
 * ed by zzy on 2017/12/10.
 */
@ControllerAdvice
public class ExceptionHandle {

    Logger logger= LoggerFactory.getLogger(ExceptionHandle.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        logger.error(e.getMessage(),e);
        if(e instanceof GrilExecption){
            GrilExecption grilExecption= (GrilExecption) e;
            return ResultUtil.error(grilExecption.getCode(),grilExecption.getMessage());
        }
        return ResultUtil.error(-1,"未知错误");
    }
}
